package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.MapCity;
import com.ruoyi.system.domain.MapCompositeIndex;
import com.ruoyi.system.mapper.MapCityMapper;
import com.ruoyi.system.mapper.MapCompositeIndexMapper;
import com.ruoyi.system.service.IMapCompositeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 城市发展综合指数Service业务层处理
 *
 * @author cqust_icc
 * @date 2022-06-23
 */
@Service
public class MapCompositeIndexServiceImpl implements IMapCompositeIndexService
{
    @Autowired
    private MapCompositeIndexMapper mapCompositeIndexMapper;
    @Autowired
    private MapCityMapper mapCityMapper;

    /**
     * 查询城市发展综合指数
     *
     * @param id 城市发展综合指数主键
     * @return 城市发展综合指数
     */
    @Override
    public MapCompositeIndex selectMapCompositeIndexById(Long id)
    {
        return mapCompositeIndexMapper.selectMapCompositeIndexById(id);
    }

    /**
     * 查询城市发展综合指数列表
     *
     * @param mapCompositeIndex 城市发展综合指数
     * @return 城市发展综合指数
     */
    @Override
    public List<MapCompositeIndex> selectMapCompositeIndexList(MapCompositeIndex mapCompositeIndex)
    {
        return mapCompositeIndexMapper.selectMapCompositeIndexList(mapCompositeIndex);
    }

    /**
     * 新增城市发展综合指数
     *
     * @param mapCompositeIndex 城市发展综合指数
     * @return 结果
     */
    @Override
    public int insertMapCompositeIndex(MapCompositeIndex mapCompositeIndex)
    {
        mapCompositeIndex.setCreateTime(DateUtils.getNowDate());
        return mapCompositeIndexMapper.insertMapCompositeIndex(mapCompositeIndex);
    }

    /**
     * 修改城市发展综合指数
     *
     * @param mapCompositeIndex 城市发展综合指数
     * @return 结果
     */
    @Override
    public int updateMapCompositeIndex(MapCompositeIndex mapCompositeIndex)
    {
        return mapCompositeIndexMapper.updateMapCompositeIndex(mapCompositeIndex);
    }

    @Override
    public int updateMapCompositeIndexByCityCode(MapCompositeIndex mapCompositeIndex) {
        return mapCompositeIndexMapper.updateMapCompositeIndexByCityCode(mapCompositeIndex);
    }

    /**
     * 批量删除城市发展综合指数
     *
     * @param ids 需要删除的城市发展综合指数主键
     * @return 结果
     */
    @Override
    public int deleteMapCompositeIndexByIds(Long[] ids)
    {
        return mapCompositeIndexMapper.deleteMapCompositeIndexByIds(ids);
    }

    /**
     * 删除城市发展综合指数信息
     *
     * @param id 城市发展综合指数主键
     * @return 结果
     */
    @Override
    public int deleteMapCompositeIndexById(Long id)
    {
        return mapCompositeIndexMapper.deleteMapCompositeIndexById(id);
    }

    @Override
    public String importData(List<MapCompositeIndex> baseList) {
        List<MapCity> mapCities = mapCityMapper.selectMapCityList(null);

        for (MapCompositeIndex item : baseList) {

            String trim = item.getCityName().trim();

            /*
             * 转换城市code
             * */
            boolean flag = true;
            for (MapCity mapCity : mapCities) {
                if (mapCity.getCityName().contains(trim)) {
                    flag = false;
                    item.setCityCode(mapCity.getCityCode());
                    break;
                }
            }

            if(flag){
                item.setCityCode("500000");
            }
            Long aLong = mapCompositeIndexMapper.selectMapCompositeIndexByCityCode(item.getCityCode());
            if (aLong>0){
                updateMapCompositeIndexByCityCode(item);
            }else{
                insertMapCompositeIndex(item);
            }
        }

        return "导入成功！";
    }


}
